// pages/my/login/login.ts
import { codeExchangeID, getUserToken } from '../../../http/api/userServiceApi'
// @ts-ignore
import Toast from 'tdesign-miniprogram/toast/index'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        url: '/assets/user/用户.png',
        showPop: false
    },
    async login(e: any) {
        const res: any = await wx.login()
        const code = res.code
        const idRes: any = await codeExchangeID(code)
        const openid = idRes.data.data[0].openid
        console.log(idRes, openid, '----openid')
        wx.setStorageSync('user-type', e.currentTarget.dataset.type)
        const tokenRes: any = await getUserToken(openid, e.currentTarget.dataset.type)
        const { api_token: token, user_info } = tokenRes.data.data[0]
        console.log(tokenRes, token, '-----token')
        wx.setStorageSync('token', token)
        wx.setStorageSync('openid', openid)
        console.log(user_info[0])
        const { user_sname, user_tel, user_icon } = user_info[0]
        if (user_sname == null || user_tel == null || user_sname == '' || user_tel == '') {
            console.log('未实名')
            wx.redirectTo({
                url: '/pages/user/userAdd/userAdd'
            })
        } else {
            this.setData({
                url: user_icon
            })
            if (e.currentTarget.dataset.type == 1) {
                wx.switchTab({
                    url: '/pages/index/index'
                })
            } else {
                wx.switchTab({
                    url: '/pages/driver/index/index'
                })
            }
        }
    },
    getPhoneNumber(e: any) {
        let detail = e.detail
        console.log(detail)
        if (detail.errMsg === 'getPhoneNumber:ok') {
            let code = detail.code // 动态令牌
            console.log(code, '用户同意授权')
        } else {
            console.log('用户拒绝授权')
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad() {
        const privacySettingRes: any = await this.getPrivacySetting()
        console.log('privacySettingRes :>> ', privacySettingRes)
        this.setData({
            showPop: privacySettingRes.needAuthorization
        })
    },

    /**
     * 按钮点击回调
     */
    popBtnTap(res) {
        console.log('授权结果返回数据 :>> ', res)
        console.log('授权结果 :>> ', res.detail)
        if (res.detail.result) {
            wx.showToast({
                title: '同意授权',
                icon: 'success'
            })
        } else {
            wx.switchTab({
                url: '/pages/index/index'
            })
            wx.showToast({
                title: '拒绝授权',
                icon: 'error'
            })
        }
    },

    /**
     * 获取隐私协议授权信息
     * @returns {object} {needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》'}
     */
    getPrivacySetting() {
        const res = {
            needAuthorization: true,
            privacyContractName: '基础库过低，不需要授权'
        }
        if (!wx.getPrivacySetting) return res
        return new Promise((resolve, reject) => {
            wx.getPrivacySetting({
                success(res) {
                    resolve(res)
                },
                fail(err) {
                    reject(err)
                }
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {}
})
